// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-05 13:21
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.Greedy;

import java.util.Arrays;
import java.util.HashSet;

public class LongestConsecutive {
    /**
     * 获取数字的连续最长序列
     * 使用Hash表-> 在O(1)的时间复杂度下寻找元素
     *
     * @param nums
     * @return
     */


    public int longestConsecutive(int[] nums) {
        HashSet<Integer> num_set = new HashSet<>();
        int longestConsecutiveNum = 0;
        for (int num : nums) {
            // 使用HashSet进行去重
            num_set.add(num);
        }
        for (int num : num_set) {
            if (!num_set.contains(num - 1)) {
                int curNum = num;
                int curLong = 1;
                // 如果包含下面的元素则直接进行跳过，元素连续判断通过往上判断
                while (num_set.contains(curNum + 1)) {
                    curNum++;
                    curLong++;
                }
                longestConsecutiveNum = Math.max(curLong, longestConsecutiveNum);
            }
        }
        return longestConsecutiveNum;
    }
}
